/*
 * (C) Copyright 2009
 * Sean Chiang, <n2i911@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef OS_CPU_H
#define OS_CPU_H

typedef unsigned char	INT8U;
typedef unsigned short	INT16U;
typedef unsigned int	INT32U;

typedef signed char	INT8S;
typedef signed short	INT16S;
typedef signed int	INT32S;

typedef INT8U		BOOLEAN;

typedef INT32U		OS_STK;

/* High to low memory stack growth */
#define OS_STK_GROWTH	1

#if OS_CRITICAL_METHOD == 3
typedef INT32U		OS_CPU_SR;

#define OS_ENTER_CRITICAL()			\
        /*					\
         * Disable both IRQ and FIQ		\
         */					\
        __asm__ __volatile__(			\
	         "mrs   %0, cpsr\n\t"		\
                 "orr   r8, %1, #0xC0\n\t"	\
                 "msr   cpsr_c, r8\n\t"		\
                 : "=r" (cpu_sr)		\
                 : "r"	(cpu_sr)		\
                 : "memory");

#define OS_EXIT_CRITICAL()			\
        /*					\
         * Restore the cpsr(IRQ and FIQ)	\
         */					\
        __asm__ __volatile__(			\
                 "msr   cpsr_c, %0\n\t"		\
                 :				\
                 : "r" (cpu_sr) 	        \
                 : "memory");
#else
#define OS_ENTER_CRITICAL()			\
        /*					\
         * Disable both IRQ and FIQ		\
         */					\
        __asm__ ("mrs   r1, cpsr\n\t"		\
                 "orr   r1, r1, #0xC0\n\t"	\
                 "msr   cpsr_c, r1\n\t");


#define OS_EXIT_CRITICAL()			\
        /*					\
         * Enable both IRQ and FIQ		\
         */					\
        __asm__ ("mrs   r1, cpsr\n\t"		\
                 "bic   r1, r1, #0xC0\n\t"	\
                 "msr   cpsr_c, r1\n\t");
#endif

#define OS_TASK_SW()				\
	/*					\
	 * Trigger a software interrupt		\
	 * by calling swi instruction		\
	 * Why 911? That's my birthday		\
	 */					\
	__asm__ ("swi	0x911\n\t");

void	OSStartHighRdy	(void);
void	OSIntCtxSw	(void);

#endif
